package controller;

import helper.Postman;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import databeans.Customer;
import databeans.Employee;
import model.EmployeeDAO;
import model.Model;

public class TestSendEmailAction extends Action{
    //private EmployeeDAO employeeDAO;

    public TestSendEmailAction(Model model) {
        //employeeDAO = model.getEmployeeDAO();
    }
    
    public String getName() {
        return "testSendEmail.do";
    }
    

    public String perform(HttpServletRequest request) {
        //ATTENTION
        // define target page

        String originPage = "send-email.jsp";
        String successPage = "send-email.jsp"; 
        
        //set the employee
        //Employee employee = null;
        //Customer customer = null;

        //errors and messages will be stored here
        //resetting those containers
        List<String> errors = new ArrayList<String>();
        List<String> messages = new ArrayList<String>();
   
        //define the error and message
        request.setAttribute("errors", errors);
        request.setAttribute("messages", messages);
        
        //get all the parameter from the form
        String requestType = request.getParameter("Submit");
        
        String email = request.getParameter("txtEmail");
        String message = request.getParameter("txtMessage");

        //reload the form field
        request.setAttribute("txtEmail", email);
        request.setAttribute("txtMessage", message);
  
        
        //check if employee has been logged in
        //request.getSession() will return the current session and if one does not exist, a new session will be cretaed.
        //request.getSession(true) will return the current session if one exists, if one doesn't exits a new one will be created.
        //So there is actually no difference between the two methods HOWEVER, if you use request.getSession(false), it will return the current session if one exists and if one DOES NOT exist a new one will NOT be cretaed.
     
        //No validation necessary
        
        //validate if user has necessary qualification
        //check the session
        //No validation necessary

        // check for get or post request
        //if there is no parameter get passed, return to origin page
        if (requestType == null) {
            messages.add("To send email, fill the below form");
            return originPage;     
        }
        
        //validate the input
        if (email.trim().equals("")) {
            messages.add("You need email address");
            errors.add("Current email field is still empty");
            return originPage;
        }
        
        if (message.trim().equals("")) {
            messages.add("You must have proper message");
            errors.add("Current message field is still empty");
            return originPage;
        }
        
   
        
        // at this stage, all validation should have been done
        try {
            
  
              String smtpServer="smtp.gmail.com";
              String to="alesmana@gmail.com";  
              String from="majesticCFAnoreply@gmail.com";
              String subject="Test message subject";
              String body="This is body";
              Postman.send(smtpServer, to, from, subject, body);

           
            // Attach (this copy of) the logged in employee bean to the session
            //HttpSession session = request.getSession(true); 
            //Employee loggedInEmployee = (Employee) session.getAttribute("employee");
            //session.setAttribute("employee", loggedInEmployee );
            
            //set the welcome message
            messages.add("Hi!, you have succesfully crated message to " + email + " and message " + message);
            return successPage;

        } catch (Exception e) {
            System.out.println("Usage: java com.lotontech.mail.SimpleSender"
                    +" smtpServer toAddress fromAddress subjectText bodyText");
            errors.add("Something is not right");
            return originPage;
        }

    }
}